package api

import (
	"FeedXmh/app/dao"
	"fmt"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var Dictionary = dictionary{}

type dictionary struct{}

func (a *dictionary) GET(r *ghttp.Request) {
	res := g.Map{
		"code":  0,
		"msg":   "",
		"count": 0,
		"data":  nil,
	}
	where := " 1=1 "
	params := r.GetQueryMap()
	id := gconv.String(params["id"])
	if id != "" {
		where += fmt.Sprintf(" and id=%s", id)
	}
	code := gconv.String(params["code"])
	if code != "" {
		where += fmt.Sprintf(" and pid = ( SELECT id FROM t_dictionary WHERE `code`='%s')", code)
	}

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, userid) {
		data, err := db.Model("t_dictionary").Where(where).Order(" `order` asc").All()
		if err != nil {
			res["code"] = -1
			res["msg"] = err.Error()
		} else {
			res["code"] = 0
			res["data"] = data.List()
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *dictionary) PUT(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	params := r.GetFormMap()

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, userid) {
		sqlres, _ := db.Model("t_dictionary").Data(params).Where("id=?", params["id"]).Update()
		if a, err := sqlres.RowsAffected(); err == nil && a >= 0 {
			res["code"] = 1
			res["msg"] = "保存成功"
		} else {
			res["code"] = -1
			res["msg"] = "保存失败"
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *dictionary) POST(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	params := r.GetFormMap()
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, userid) {
		//判断是否已经存在
		count, _ := db.Model("t_dictionary").Where("code=? and pid=0 ", params["code"]).Count()
		if count > 0 {
			res["code"] = -1
			res["msg"] = "已经存在"
		} else {
			delete(params, "id")
			sqlres, _ := db.Model("t_dictionary").Data(params).Insert()
			if a, err := sqlres.RowsAffected(); err == nil && a > 0 {
				res["code"] = 1
				res["msg"] = "保存成功"
			} else {
				res["code"] = -1
				res["msg"] = "保存失败"
			}
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *dictionary) DELETE(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	id := r.GetString("rid")
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, userid) {
		sqlres, _ := db.Model("t_dictionary").Where("id=? or pid=?", id, id).Delete()
		if a, err := sqlres.RowsAffected(); err == nil && a >= 0 {
			res["code"] = 1
			res["msg"] = "保存成功"
		} else {
			res["code"] = -1
			res["msg"] = "保存失败"
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}
